import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:video_player/video_player.dart';

class FullScreenVideoPlayerView extends StatefulWidget {
  final VideoPlayerController controller;

  FullScreenVideoPlayerView(this.controller);

  @override
  State<StatefulWidget> createState() => _FullScreenVideoPlayerView();
}

class _FullScreenVideoPlayerView extends State<FullScreenVideoPlayerView> {
  bool _showTools = false;

  double _currentSpeed = 1.0;

  @override
  void initState() {
    super.initState();
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeRight,
      DeviceOrientation.landscapeLeft,
    ]);
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky);
    this._lazyHiddenTools();
  }

  @override
  void dispose() {
    super.dispose();
    SystemChrome.setPreferredOrientations([]);
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body:  GestureDetector(
        child: Center(
          child: Stack(
            children: [
              AspectRatio(
                aspectRatio: widget.controller.value.aspectRatio,
                child: VideoPlayer(widget.controller),
              ),
              if (!widget.controller.value.isPlaying)
                Positioned(
                  top: Get.height / 2.7,
                  width: MediaQuery.of(context).size.width,
                  child: Center(
                    child: IconButton(
                      onPressed: () {
                        this._pauseOrPlay();
                      },
                      icon: Icon(
                        size: 50,
                        Icons.play_circle,
                        color: Colors.white,
                      ),
                    ),
                  ),
                ),
              Positioned(
                top: 10,
                left: 0,
                child: AnimatedOpacity(
                  opacity: this._showTools ? 1.0 : 0.0,
                  duration: Duration(seconds: 1),
                  child: IconButton(
                    icon: Icon(
                      Icons.fullscreen_exit,
                      color: Colors.white,
                    ),
                    onPressed: () {
                      Navigator.pop(context); // 点击退出按钮返回
                    },
                  ),
                ),
              ),
              Positioned(
                child: AnimatedOpacity(
                  opacity: this._showTools ? 1.0 : 0.0,
                  duration: Duration(seconds: 1),
                  child: Container(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Flexible(
                          flex: 1,
                          child: GestureDetector(
                            onTap: () {
                              this._pauseOrPlay();
                            },
                            child: Container(
                              child: Icon(
                                widget.controller.value.isPlaying
                                    ? Icons.pause
                                    : Icons.play_arrow,
                                color: Colors.white,
                              ),
                              padding: EdgeInsets.only(
                                right: 10,
                              ),
                            ),
                          ),
                        ),
                        Flexible(
                          flex: 10,
                          child: Container(
                            child: VideoProgressIndicator(
                              padding: EdgeInsets.zero,
                              widget.controller,
                              allowScrubbing: true,
                            ),
                            height: 8,
                          ),
                        ),
                        Flexible(
                          flex: 1,
                          child: GestureDetector(
                            onTapDown: (TapDownDetails details) {
                              this._showContextMenu(
                                  context, details.globalPosition);
                            },
                            child: Container(
                              child: Text(
                                "x${this._currentSpeed}",
                                style: TextStyle(
                                  color: Colors.white,
                                ),
                              ),
                              padding: EdgeInsets.only(
                                left: 10,
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                    width: Get.width,
                  ),
                ),
                bottom: 10,
                left: 0,
              ),
            ],
          ),
        ),
        onTap: () {
          if (this._showTools == false) {
            setState(() {
              this._showTools = true;
            });
            this._lazyHiddenTools();
          }
        },
      ),
    );
  }

  _lazyHiddenTools() {
    Future.delayed(Duration(seconds: 3), () {
      if (this._showTools) {
        setState(() {
          this._showTools = false;
        });
      }
    });
  }

  // _showSpeedMenu 显示速度控制菜单栏
  void _showContextMenu(BuildContext context, Offset tapPosition) async {
    final RenderBox overlay =
        Overlay.of(context).context.findRenderObject() as RenderBox;

    await showMenu(
      context: context,
      position: RelativeRect.fromRect(
        tapPosition & Size.zero, // 使用点击位置
        Offset.zero & overlay.size,
      ),
      color: Colors.black.withOpacity(0.8),
      items: [
        PopupMenuItem<double>(
          value: 1.0,
          child: Center(
            child: Text(
              '1.0',
              style: TextStyle(color: Colors.white),
            ),
          ),
        ),
        PopupMenuItem<double>(
          value: 1.5,
          child: Center(
            child: Text(
              '1.5',
              style: TextStyle(color: Colors.white),
            ),
          ),
        ),
        PopupMenuItem<double>(
          value: 2,
          child: Center(
            child: Text(
              '2.0',
              style: TextStyle(color: Colors.white),
            ),
          ),
        ),
      ],
      elevation: 8.0,
    ).then(
      (value) {
        if (value != null) {
          setState(() {
            this._currentSpeed = value;
          });
          widget.controller.setPlaybackSpeed(value);
        }
      },
    );
  }

  void _pauseOrPlay() {
    if (!widget.controller.value.isPlaying) {
      widget.controller.play();
    } else {
      widget.controller.pause();
    }
    setState(() {});
  }
}
